package com.tongtailian.www.stir.bean;

import com.tongtailian.www.stir.utils.LogUtil;

/**
 * @author liuml
 * @explain
 * @time 2020/10/11 12:53
 */
public class DeviceUseFram extends BaseDataFram {


    private int stop = 0;
    private int group = 0;//一共63组  0 - 62
    private int loop = 0;
    //    工作位选择，位有效，1 选中
//            Bit0 =从左向右，左边第一个工作位。
//    Bit1 =从左向右，左边第一个工作位。
//    Bit2 =从左向右，左边第一个工作位。
//    Bit3 =从左向右，左边第一个工作位。
    private int workLocation = 0;//样品座选择 工作位选择
    //    int one = 0x01;
//    int two = 0x02;
//    int three = 0x03;
//    int four = 0x04;
    private int workLocation1 = 01;
    private int workLocation2 = 02;
    private int workLocation3 = 03;
    private int workLocation4 = 04;

    private int group_workLocation1 = 0;
    private int group_workLocation2 = 0;
    private int group_workLocation3 = 0;
    private int group_workLocation4 = 0;

    //加1 加 2  加4  加8

    /**
     * @param param 共有8套 从80 到87
     * @param data
     */
    public DeviceUseFram(String param, String data) {
        this.type = BaseDataFramType.devicesUse;
        this.param = param;
        this.data = data;
//        super("70", param, data);
    }

    public DeviceUseFram() {
        this.type = BaseDataFramType.devicesUse;
//        super("70", param, data);
    }


    public String createData() {
        StringBuilder stringBuilder = new StringBuilder();
        String myGroup = int2HexFormat(group, data2);
        LogUtil.d("第几组: " + myGroup);
        stringBuilder.append(myGroup);
        String looper = int2HexFormat(loop, data2);
        LogUtil.d("looper: " + looper);
        stringBuilder.append(looper);
        //对应位置给出的数据是编号：0x01, 0x02, 0x03, 0x04
        //开始
        String hexString = binaryToDecimal(workLocation1);
        String hexString2 = binaryToDecimal(workLocation2);
        String hexString3 = binaryToDecimal(workLocation3);
        String hexString4 = binaryToDecimal(workLocation4);
        workLocation = workLocation1 + workLocation2 + workLocation3 + workLocation4;
        LogUtil.d(("workLocation 样品座 = " + workLocation));
        String workLocationStr = int2HexFormat(workLocation, data4);
        LogUtil.d("workLocationStr : " + workLocationStr);
        stringBuilder.append(workLocationStr);
        this.data = stringBuilder.toString();
        final String sendData = getSendData();
        LogUtil.d("设备最终发送数据 = " + sendData);
        return sendData;
    }

    public String createConfirmOrCancleData() {
        StringBuilder stringBuilder = new StringBuilder();
        String looper = int2HexFormat(loop, data2);
        LogUtil.d("looper: " + looper);
        stringBuilder.append(looper);

        workLocation = workLocation1 + workLocation2 + workLocation3 + workLocation4;
        LogUtil.d(("workLocation 样品座 = " + workLocation));
        String workLocationStr = int2HexFormat(workLocation, data4);
        LogUtil.d("workLocationStr : " + workLocationStr);
        stringBuilder.append(workLocationStr);

        this.data = stringBuilder.toString();
        final String sendData = getSendData();
        LogUtil.d("设备最终发送数据 = " + sendData);
        return sendData;
    }
    public String createDataForCustom() {
        StringBuilder stringBuilder = new StringBuilder();
        String byte1 = "00";
        LogUtil.d("独立工作模式第一位保留: " + byte1);
        stringBuilder.append(byte1);
        String looper = int2HexFormat(loop, data2);
        LogUtil.d("looper: " + looper);
        stringBuilder.append(looper);
        //对应位置给出的数据是编号：0x01, 0x02, 0x03, 0x04
        //开始
        String hexString = binaryToDecimal(workLocation1);
        String hexString2 = binaryToDecimal(workLocation2);
        String hexString3 = binaryToDecimal(workLocation3);
        String hexString4 = binaryToDecimal(workLocation4);
        workLocation = workLocation1 + workLocation2 + workLocation3 + workLocation4;
        LogUtil.d(("workLocation 样品座 = " + workLocation));
        String workLocationStr = int2HexFormat(workLocation, data2);
        LogUtil.d("workLocationStr : " + workLocationStr);
        stringBuilder.append(workLocationStr);

        String byte3 = "00";
        LogUtil.d("独立工作模式byte3保留: " + byte1);
        stringBuilder.append(byte3);

        String group = int2HexFormat(group_workLocation1, data2)+
                int2HexFormat(group_workLocation2, data2)+
                int2HexFormat(group_workLocation3, data2)+
                int2HexFormat(group_workLocation4, data2);
        LogUtil.d("独立工作模式group : " + group);

        stringBuilder.append(group);
        this.data = stringBuilder.toString();
        final String sendData = getSendData();
        LogUtil.d("设备最终发送数据 = " + sendData);
        return sendData;
    }


    public String binaryToDecimal(int n) {
        String result = Integer.toBinaryString(n);
        return result;
    }

    @Override
    public void parseFrame(String frame) {
        super.parseFrame(frame);
        int position = 0;

        position = position + data2;
        //具体解析
        LogUtil.d("解析数据:" + data);
        //32  字符
    }

    public int getStop() {
        return stop;
    }

    public void setStop(int stop) {
        this.stop = stop;
    }

    public int getWorkLocation1() {
        return workLocation1;
    }

    public void setWorkLocation1(int workLocation1) {
        this.workLocation1 = workLocation1;
    }

    public int getWorkLocation2() {
        return workLocation2;
    }

    public void setWorkLocation2(int workLocation2) {
        this.workLocation2 = workLocation2;
    }

    public int getWorkLocation3() {
        return workLocation3;
    }

    public void setWorkLocation3(int workLocation3) {
        this.workLocation3 = workLocation3;
    }

    public int getWorkLocation4() {
        return workLocation4;
    }

    public void setWorkLocation4(int workLocation4) {
        this.workLocation4 = workLocation4;
    }

    public int getLoop() {
        return loop;
    }

    public void setLoop(int loop) {
        this.loop = loop;
    }

    public int getGroup() {
        return group;
    }

    public void setGroup(int group) {
        this.group = group;
    }

    public int getWorkLocation() {
        return workLocation;
    }

    public void setWorkLocation(int workLocation) {
        this.workLocation = workLocation;
    }

    public int getGroup_workLocation1() {
        return group_workLocation1;
    }

    public void setGroup_workLocation1(int group_workLocation1) {
        this.group_workLocation1 = group_workLocation1;
    }

    public int getGroup_workLocation2() {
        return group_workLocation2;
    }

    public void setGroup_workLocation2(int group_workLocation2) {
        this.group_workLocation2 = group_workLocation2;
    }

    public int getGroup_workLocation3() {
        return group_workLocation3;
    }

    public void setGroup_workLocation3(int group_workLocation3) {
        this.group_workLocation3 = group_workLocation3;
    }

    public int getGroup_workLocation4() {
        return group_workLocation4;
    }

    public void setGroup_workLocation4(int group_workLocation4) {
        this.group_workLocation4 = group_workLocation4;
    }

    public static String encode(String str) {
        //根据默认编码获取字节数组
        byte[] bytes = str.getBytes();
        StringBuilder sb = new StringBuilder(bytes.length * 2);
        //将字节数组中每个字节拆解成2位16进制整数
        for (int i = 0; i < bytes.length; i++) {
            sb.append(hexString.charAt((bytes[i] & 0xf0) >> 4));
            sb.append(hexString.charAt((bytes[i] & 0x0f) >> 0));
        }
        return sb.toString();
    }

    @Override
    public String toString() {
        return "DeviceUseFram{" +
                "stop=" + stop +
                ", group=" + group +
                ", loop=" + loop +
                ", workLocation=" + workLocation +
                ", workLocation1=" + workLocation1 +
                ", workLocation2=" + workLocation2 +
                ", workLocation3=" + workLocation3 +
                ", workLocation4=" + workLocation4 +
                ", group_workLocation1=" + group_workLocation1 +
                ", group_workLocation2=" + group_workLocation2 +
                ", group_workLocation3=" + group_workLocation3 +
                ", group_workLocation4=" + group_workLocation4 +
                '}';
    }

    private static String hexString = "0123456789ABCDEF";


}
